// miniprogram/pages/mall/mall.js
var amapFile = require('../../lib/amap-wx.js');
var markersData = {
  latitude:'',
  longitude:'',
  key:"a53e2b35cf90ed37f1c97c99edf2cad2"
}


const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    commodity:[],
    location:"",
    test:true,
  },
  loadInfo:function(){
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        var latitude = res.latitude;
        var longtitude = res.longitude;
        that.loadCity(latitude,longtitude);
      }
    })
  },
  loadCity:function(lat,lon){
    var that = this;
    var myAmapFunction = new amapFile.AMapWX({key:markersData.key});
    myAmapFunction.getRegeo({
      location:''+lon+','+lat+'',
      success: function(data){
        console.log(data);
        var location =
        data[0].regeocodeData.addressComponent.city;

        that.setData({
          location:location
        })
        app.globalData.location = location
      },
      fail:function(info){
        console.log(info);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadInfo();
    this.queryAllCommodity(function(){
      wx.hideLoading({
        success: (res) => {},
      })
    })
    if(app.globalData.location){
      this.setData({
        location:app.globalData.location
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setTabBar()
  },
  setTabBar:function(){
    const length = app.globalData.carts.reduce((sum,a)=>sum+a.num,0);
    if(length > 0){
      wx.setTabBarBadge({
        index: 2,
        text: length+'',
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  queryAllCommodity: function(callback) {
    wx.showLoading({
      title:'查询中',
    })
    const db = wx.cloud.database()
    // 查询所有的商品
    db.collection('commodity').where({}).get({
      success: res => {
        app.globalData.commodity = res.data.reverse()
        console.log(app.globalData)
        this.setData({
          commodity:app.globalData.commodity
        },function(){
          callback()
        })
        console.log('[数据库] [查询记录] 成功: ', res)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败：', err)
      }
    })
  },
  //跳转到详情页面
  go:function(e){
    var tapIndex = e.currentTarget.dataset.index;
    var commodity_id = this.data.commodity[tapIndex]._id;
    wx.showLoading({
      title: '加载中',
    })
    wx.navigateTo({
      url: '../commodity/commodity?id='+commodity_id+'&&tapIndex='+tapIndex,
    })
    wx.hideLoading({
      success: (res) => {},
    })
  }
})